package org.csu.mybigpro.controller;

import org.csu.mybigpro.DTO.PlagiarismReportDto;
import org.csu.mybigpro.service.PlagiarismService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
// 【修复】移除类级别的@RequestMapping
public class PlagiarismController {

    @Autowired
    private PlagiarismService plagiarismService;

    // 【修复】将API的完整路径直接定义在@PostMapping注解中
    @PostMapping("/api/plagiarism/analyze")
    public ResponseEntity<?> analyzeSubmissions(@RequestParam("files") List<MultipartFile> files) {
        if (files == null || files.isEmpty()) {
            return ResponseEntity.badRequest().body(Map.of("error", "请上传需要分析的文件。"));
        }
        try {
            List<PlagiarismReportDto> report = plagiarismService.analyzeSubmissions(files);
            return ResponseEntity.ok(report);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.internalServerError().body(Map.of("error", "分析过程中出现意外错误: " + e.getMessage()));
        }
    }
}